package Ticket;

import Ticket.VO.EmailVO;
import Ticket.VO.ResultVO;
import Utils.SendMailTest;
import Utils.SslUtils;
import com.alibaba.fastjson.JSON;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

/**
 * @Description :
 * @author: cww
 * @DateTime: 2019/2/1 17:00
 */

public class GetTicket {

    /**
     * 发起一个http请求
     */
    @Test
    public void getTicket(){

        URL url;
        int responsecode;
        HttpURLConnection urlConnection;
        BufferedReader reader;
        String line;
        try{
            //忽略Ssl(针对12306)
            SslUtils.ignoreSsl();
            //生成一个URL对象
            url=new URL("https://kyfw.12306.cn/otn/leftTicket/queryZ?leftTicketDTO.train_date=2019-02-13&leftTicketDTO.from_station=NES&leftTicketDTO.to_station=XMS&purpose_codes=ADULT");
            /**
             * 这是为了防止12306对同一ip多次访问进行限制
             * 这里填的ip是暂时有效的，想要获取更多就得自己去找 搜索代理ip
             */
            System.getProperties().setProperty("proxySet", "true");
            System.setProperty("http.proxyHost", "120.78.15.63");
            System.setProperty("http.proxyPort", "80");
            //打开URL
            urlConnection = (HttpURLConnection)url.openConnection();
            //伪造一个请求头 一般网页不用，有些网站会看你有没有请求头，比如 12306......
            urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0");
            urlConnection.setRequestProperty("Host","kyfw.12306.cn");
            urlConnection.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            urlConnection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
            urlConnection.setRequestProperty("Accept-Encoding","identity");
            urlConnection.setRequestProperty("Connection","keep-alive");
            urlConnection.setRequestProperty("Upgrade-Insecure-Requests","1");
            //获取服务器响应代码
            responsecode=urlConnection.getResponseCode();
            //假如响应代码为200，就是代表成功
            if(responsecode==200){
                reader=new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
                while((line=reader.readLine())!=null){
                    ResultVO resultVO ;
                    resultVO = JSON.parseObject(line, ResultVO.class);
                    boolean isSendEmail = false;
                    StringBuilder stringBuilder = new StringBuilder();
                    for (String resultStr:resultVO.getData().getResult()){
                        List<String> strings = Arrays.asList(resultStr.split("\\|"));
                        if ("D6333".equals(strings.get(3))){
                            System.out.println("D6333：16:41-19:19 02:38");
                            System.out.println("无座："+strings.get(26));
                            System.out.println("二等座："+strings.get(30));
                            System.out.println("一等座："+strings.get(31));
                            if ((!"无".equals(strings.get(26)))||(!"无".equals(strings.get(30)))||(!"无".equals(strings.get(31)))){
                                stringBuilder.append("D6333：16:41-19:19 02:38"+"<br>"+"无座："+strings.get(26)+"<br>"+"二等座："+strings.get(30)+"<br>"+"一等座："+strings.get(31)+"<br>");
                                isSendEmail = true;
                            }
                        } else if("D2293".equals(strings.get(3))){
                            System.out.println("D2293：17:17-19:17 02:00");
                            System.out.println("无座："+strings.get(26));
                            System.out.println("二等座："+strings.get(30));
                            System.out.println("一等座："+strings.get(31));
                            if ((!"无".equals(strings.get(26)))||(!"无".equals(strings.get(30)))||(!"无".equals(strings.get(31)))){
                                stringBuilder.append("D2293：17:17-19:17 02:00"+"<br>"+"无座："+strings.get(26)+"<br>"+"二等座："+strings.get(30)+"<br>"+"一等座："+strings.get(31)+"<br>");
                                isSendEmail = true;
                            }
                        } else if("D3215".equals(strings.get(3))){
                            System.out.println("D3215：17:35-19:53 02:18");
                            System.out.println("无座："+strings.get(26));
                            System.out.println("二等座："+strings.get(30));
                            System.out.println("一等座："+strings.get(31));
                            if ((!"无".equals(strings.get(26)))||(!"无".equals(strings.get(30)))||(!"无".equals(strings.get(31)))){
                                stringBuilder.append("D3215：17:35-19:53 02:18"+"<br>"+"无座："+strings.get(26)+"<br>"+"二等座："+strings.get(30)+"<br>"+"一等座："+strings.get(31)+"<br>");
                                isSendEmail = true;
                            }
                        } else if("D9611".equals(strings.get(3))){
                            System.out.println("D9611：16:04-19:28 03:24");
                            System.out.println("无座："+strings.get(26));
                            System.out.println("二等座："+strings.get(30));
                            System.out.println("一等座："+strings.get(31));
                            if ((!"无".equals(strings.get(26)))||(!"无".equals(strings.get(30)))||(!"无".equals(strings.get(31)))){
                                stringBuilder.append("D9611：16:04-19:28 03:24"+"<br>"+"无座："+strings.get(26)+"<br>"+"二等座："+strings.get(30)+"<br>"+"一等座："+strings.get(31)+"<br>");
                                isSendEmail = true;
                            }
                        }
//                            System.out.println(resultStr);
                    }
                    if (isSendEmail){
                        EmailVO emailVO = new EmailVO();
                        emailVO.setEmailAddress("294147287@qq.com");
                        emailVO.setEmailSubject("刷到余票");
                        emailVO.setEmailContent(stringBuilder.toString());
                        SendMailTest sendMailTest = new SendMailTest();
                        sendMailTest.SendMailTest(emailVO);
//                        emailVO.setEmailAddress("2807319404@qq.com");
//                        sendMailTest.SendMailTest(emailVO);
                    }else {

                    }
//                        System.out.println(JSON.toJSONString(resultVO));//在这里干你想干的事情
                }
            }else{
                System.out.println("获取不到网页的源码，服务器响应代码为："+responsecode);
            }
        }catch(Exception e){
            System.out.println("获取不到网页的源码,出现异常："+e);
        }
    }


}
